package com.code.leetcode.offer;

import com.code.leetcode.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 15:27
 */
public class The34 {

    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        dfs(root, sum);
        return lists;
    }

    private void dfs(TreeNode node, int target) {
        if (node == null) return;
        target -= node.val;
        list.add(node.val);
        if (target == 0 && node.left == null && node.right == null) {
            lists.add(new ArrayList<>(list));
        }
        dfs(node.left, target);
        dfs(node.right, target);
        list.remove(list.size() - 1);
    }

}
